package com.ym9527.controller;


import com.ym9527.entity.User;
import com.ym9527.jpa.UseJpa;
import com.ym9527.utils.R;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

//这是用户相关控制器
@RestController
@RequestMapping("user")
public class UserController {

  @Resource
  UseJpa useJpa;



  /**
   * 这是用户登录
   * @return
   */
  @PostMapping("signin")
  @Transactional()
  public R UserSignIn(@RequestBody User user){

    if(null!=user.getUserName()&&!user.getUserName().equals("")){

      User user1=useJpa.findByUserName(user.getUserName());

      if(null!=user1&&null!=user1.getUserName()){

        if(user1.getPassword().equals(user.getPassword())){

          if(user1.getStatus().equals("正常")){

            return R.ok("登录成功!");

          }else{

            return R.error("当前账号已经被禁用!");
          }



        }else{

          return R.error("密码错误");

        }


      }else{

        return R.error("当前账号不存在");

      }

    }else{
      return R.error("请确定当前账户名正确不能为空");
    }




  }

  /**
   * 这是用户注册
   * @return
   */
  @PostMapping("signup")
  @Transactional()
  public R UserSignUp(@RequestBody User user){


    if(null!=user.getUserName()&&null!=user.getPassword()){

      if(!"".equals(user.getUserName())&&!"".equals(user.getPassword())){

        useJpa.save(user);

      }else{
        return R.error("当前账户名或者密码不能为空字符");
      }

    }else{

      return R.error("当前账户名或者密码不能为空");

    }


    return new R();
  }


  /**
   * 这是添加用户
   * @param user
   * @return
   */
  @PostMapping("adduser")
  @Transactional()
  public R AddUser(@RequestBody User user){


    if(null!=user.getUserName()&&null!=user.getPassword()){

      if(!"".equals(user.getUserName())&&!"".equals(user.getPassword())){

        User userS =useJpa.findByUserName(user.getUserName());

        if(userS!=null){

          return R.error("当前用户已经存在");

        }else{
          user.setStatus("正常");
          useJpa.save(user);
          return  R.ok();
        }



      }else{
        return R.error("当前账户名或者密码不能为空字符");
      }

    }else{

      return R.error("当前账户名或者密码不能为空");

    }



  }


  /**
   * 这是禁用用户
   * @param user
   * @return
   */
  @PostMapping("forbiddenuser")
  @Transactional()
  public R ForbiddenUser(@RequestBody User user){

    User userZ=useJpa.getById(user.getUserId());
    userZ.setStatus("禁用");
    useJpa.save(userZ);
    return R.ok();
  }

  @PostMapping("relieveforbiddenuser")
  @Transactional()
  public R RelieveForbiddenUser(@RequestBody User user){


    User userZ=useJpa.getById(user.getUserId());
    userZ.setStatus("正常");
    useJpa.save(userZ);
    return R.ok();
  }


  /**
   * 获取所有用户
   * @param
   * @return
   */
  @GetMapping("getuserall")
  @Transactional()
  public R GetUserAll(){


    List<User> userList=useJpa.findAll();

    Collections.reverse(userList);

    return R.ok(userList);
  }



}
